package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.CategoryMapper;
import cn.tedu.csmall.product.pojo.dto.CategoryDTO;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.CategoryService;
import cn.tedu.csmall.product.web.ServiceCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 程旭东
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void add(CategoryStandardVO categoryStandardVO) {
        String name = categoryStandardVO.getName();
        CategoryStandardVO categoryStandardVO1 = categoryMapper.selectByName(name);
        if(categoryStandardVO1!=null){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"添加类别失败，已存在同名类");
        }
        categoryMapper.insert(categoryStandardVO);
    }

    @Override
    public void deleteById(Long id) {
        CategoryStandardVO categoryStandardVO = categoryMapper.selectById(id);
        if(categoryStandardVO==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"删除类别失败，不存在该类别");
        }
        categoryMapper.deleteById(id);
    }

    @Override
    public void addNew(CategoryDTO categoryDTO) {
        String name = categoryDTO.getName();
        CategoryStandardVO categoryStandardVO1 = categoryMapper.selectByName(name);
        if(categoryStandardVO1!=null){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"添加类别失败，已存在同名类");
        }
        categoryMapper.insertNew(categoryDTO);
    }

    @Override
    public List<CategoryListItemVO > listByParentId(Long id) {
        List<CategoryListItemVO> lists = categoryMapper.listByParentId(id);
        return lists;
    }
}
